package com.zhixinsou.wuyang.process;

import com.alibaba.fastjson.JSON;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.util.FileUtil;
import com.zhixinsou.wuyang.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessBBT {
    public static List<AnalysisReport> getAnalysisReportPageInfo(String filePath) {
        List<AnalysisReport> reports = new ArrayList<>();
        int startPage = 1;
        File file = new File(filePath);
        String[] lines = FileUtil.pdfReader(file, startPage, startPage).split("\r\n");
        Pattern pattern = Pattern.compile("([0-9]+)\\s-\\s([0-9]+)$");
        while (lines.length > 1 && lines[0].equals("Table of Contents")) {
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    AnalysisReport report = new AnalysisReport();
                    report.setFilePath(filePath);
                    report.setFileName(file.getName());
                    report.setStartPage(Integer.parseInt(matcher.group(1)));
                    report.setEndPage(Integer.parseInt(matcher.group(2)));
                    report.setBrokerName("BB&T Capital Markets");
                    
                    reports.add(report);
                }
            }

            startPage++;
            lines = FileUtil.pdfReader(filePath, startPage, startPage).split("\r\n");
        }
        return reports;
    }

    public static void getAnalysisReportDetail(AnalysisReport report) {
        Pattern patternVersion = Pattern.compile("([0-9]{2})\\-Page([0-9]{2})");
        Matcher matcher = patternVersion.matcher(report.getFileName());
        if (matcher.find()) {
            int index = Integer.parseInt(matcher.group(1));
            int page = Integer.parseInt(matcher.group(2));
            getAnalysisReportDetailVersion(report);
//            if(year < 2007)
//                getAnalysisReportDetailVersion(report);
//            else{
//                if(year == 2007 && version < 5)
//                    getAnalysisReportDetailVersion(report);
//                else
//                    getAnalysisReportDetailVersion2(report);
//            }
        }
    }

    public static void getAnalysisReportDetailVersion(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //Ticker and Exchange
        Pattern patternTicker = Pattern.compile("([A-Z]+)–([a-zA-Z]+)–(\\$[0-9]+.*)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherTicker = patternTicker.matcher(line);
            if (matcherTicker.find()) {
                report.setTicker(matcherTicker.group(1));
                report.setExchange(matcherTicker.group(2));
                if(!line.contains("(")){
                    int j = i - 1;
                    while(StringUtil.isEmpty(lines[j].trim()) && j >= 0)
                        j--;
                    System.out.println("Set company name here999");
                    report.setCompanyName(lines[j].trim());
                    int k = i + 1;
                    while(StringUtil.isEmpty(lines[k].trim()) && k < lines.length){
                        k++;
                    }
                    String ratingLine = lines[k].trim();
                    System.out.println("The rating line is " + ratingLine);
                    String[] words = ratingLine.split("\\s");
                    if(words.length >= 2)
                        report.setRating(words[words.length - 2] + words[words.length - 1]);
                }
                break;
            }
        }

        if(report.getTicker() == null || report.getTicker().equals("")){
            patternTicker = Pattern.compile("\\(([A-Z]+)–([a-zA-Z]+)–(\\$[0-9]+.*)\\)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherTicker = patternTicker.matcher(line);
                if (matcherTicker.find()) {
                    report.setTicker(matcherTicker.group(1));
                    report.setExchange(matcherTicker.group(2));
                    report.setInitiatingCoverage(lines[i + 1]);
                    break;
                }
            }
        }

        Pattern patternThree = Pattern.compile("(.*)\\(([A-Z]+)–(\\$[0-9]+(\\.[0-9]+)?)\\)\\s*([A-Z\\S]+)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if(StringUtil.isEmpty(line))
                continue;
            Matcher matcher = patternThree.matcher(line);
            if (matcher.find()) {

                System.out.println("Set company name here666");
                report.setCompanyName(matcher.group(1));
                report.setTicker(matcher.group(2));
                report.setRating(matcher.group(5));
                break;
            }
        }

        //Analyst name
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(),false);
        lines = content.split("\r\n");
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        Pattern patternName2 = Pattern.compile("(.*)/\\s*\\([0-9]+\\)\\s[0-9]+\\-[0-9]+\\s*/.*@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if(StringUtil.isEmpty(line))
                continue;
            Matcher matcherEmail = patternEmail.matcher(line);
            Matcher matcherName = patternName2.matcher(line);
            if (matcherEmail.find()) {
                if(matcherName.find()){
                    report.addAnalystName(matcherName.group(1));
                }else{
                    report.addAnalystName(lines[ i - 2]);
                }
            }
        }
        if(report.getAnalystNames() == null || report.getAnalystNames().size() == 0){
            Pattern patternName = Pattern.compile("(.*)\\([0-9]+\\)\\s[0-9]+\\-[0-9]+");
            patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i].trim();
                if(StringUtil.isEmpty(line))
                    continue;
                Matcher matcher = patternName.matcher(line);
                Matcher matcherDate = patternDate.matcher(line);
                if (matcher.find()) {
                    if(matcherDate.find()){
                        report.addAnalystName(matcherDate.group(2));
                    }else
                        report.addAnalystName(matcher.group(1));
                }
            }
        }

        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(),report.getStartPage(),report.getStartPage());
        lines = content.split("\r\n");
        //Company Name
        if(report.getCompanyName() == null || report.getCompanyName().equals("")){
            Pattern patternCompany = Pattern.compile("(Verdana-Bold\\]<18\\.0>)(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherCompany = patternCompany.matcher(line);
                if (matcherCompany.find()) {
                    System.out.println(line);
                    String originCompany = matcherCompany.group(2);
                    System.out.println("Set company name here111" + originCompany);
                    if(originCompany.contains("[")){
                        int end = originCompany.indexOf("[");
                        report.setCompanyName(originCompany.substring(0,end));
                    }
                    else{
                        report.setCompanyName(originCompany);
                    }
                    break;
                }
            }
        }
        //Rating
        if(report.getRating() == null || report.getRating().equals("")){
            Pattern patternRating = Pattern.compile("(Verdana-Bold\\]<12\\.0>)(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    String originRating = matcherRating.group(2);
                    if(originRating.contains("[")){
                        int end = originRating.indexOf("[");
                        report.setRating(originRating.substring(0,end));
                    }
                    else{
                        report.setRating(originRating);
                    }
                }
            }
        }


        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
        lines = content.split("\r\n");
        Pattern patternSentence = Pattern.compile("([0-9]+)[\\.]?\\s+([A-Z]+)([a-z]+)([^\r\n\\.]+\r\n)*[^\r\n\\.]*owns[\r\n\\s]+shares[\r\n]{0,2}([^\r\n\\.]+[\r\n]{0,2})*(\\.\\s|\\.\r\n)?", Pattern.MULTILINE);
        Pattern patternTable = Pattern.compile("^Company\\s*Disclosure$");
        Matcher matcher = patternSentence.matcher(content);
        boolean tableStart = false;
        while (matcher.find()) {
            String companyIndex = matcher.group(1);
            String sentence = matcher.group().replaceAll("[\r\n]", " ");
            report.addOwnershipDisclosure(sentence);

            if (StringUtil.isEmpty(companyIndex)) {
                continue;
            }

            for (String line : lines) {
                Matcher matcherTable = patternTable.matcher(line.trim());
                if (!tableStart && matcherTable.find()) {
                    tableStart = true;
                    continue;
                }

                if (tableStart) {
                    Pattern pattern = Pattern.compile("([a-zA-Z]+[0-9]*).*" + companyIndex + "[^a-zA-Z0-9]+.*");
                    Matcher m = pattern.matcher(line);
                    if (m.find()) {
                        report.addCompaniesMentioned(m.group());
                    }

                    if(line.contains("Research Disclosure Legend") || StringUtil.isEmpty(line.trim()))
                        break;

                    Pattern patternEnd = Pattern.compile("^[0-9]+");
                    Matcher matcherEnd = patternEnd.matcher(line);
                    if (matcherEnd.find()) {
                        break;
                    }
                }
            }
        }

        if(report.getOwnershipDisclosures() == null || report.getOwnershipDisclosures().size() == 0){
            content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
            lines = content.split("\r\n");
            patternSentence = Pattern.compile("(^[A-Z]+)([a-z]+)([^\r\n\\.]+\r\n)*[^\r\n\\.]*owns[\r\n\\s]+shares[\r\n]{0,2}([^\r\n\\.]+[\r\n]{0,2})*(\\.\\s|\\.\r\n)?", Pattern.MULTILINE);
            matcher = patternSentence.matcher(content);
            if (matcher.find()) {
                String sentence = matcher.group().replaceAll("[\r\n]", " ");
                report.addOwnershipDisclosure(sentence);
            }
        }

        report.setPriorRecommendation("");
        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetailVersion2(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                int j = i + 1;
                while(StringUtil.isEmpty(lines[j].trim())){
                    j++;
                }
                Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                Matcher matcherCompany = patternCompany.matcher(lines[j]);
                if(matcherCompany.find()){
                    report.setDate(matcherDate.group());
                    report.setCompanyName(matcherCompany.group(1));
                    report.setTicker(matcherCompany.group(2));
                }
                break;
            }
        }
        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the second date pattern: %s", line));
                    String year = lines[i + 1].trim();
                    Pattern patternYear = Pattern.compile("[0-9]{4}");
                    Matcher matcherYear = patternYear.matcher(year);
                    if(matcherYear.find()){
                        int j = i + 2;
                        while(StringUtil.isEmpty(lines[j].trim())){
                            j++;
                        }
                        Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                        Matcher matcherCompany = patternCompany.matcher(lines[j]);
                        if(matcherCompany.find()){
                            report.setDate(matcherDate.group() + " " + year);
                            report.setCompanyName(matcherCompany.group(1));
                            report.setTicker(matcherCompany.group(2));
                        }
                    }
                    break;
                }
            }
        }

        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("[0-9]{1,2}\\-[0-9]{1,2}\\-[0-9]{1,4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the third date pattern: %s", line));
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
                    if(matcherCompany.find()){
                        report.setDate(matcherDate.group());
                        report.setCompanyName(matcherCompany.group(1));
                        report.setTicker(matcherCompany.group(2));
                    }
                    break;
                }
            }
        }
        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("(Jan\\.|Feb\\.|Mar\\.|Apr\\.|May|Jun\\.|Jul\\.|Aug\\.|Sep\\.|Oct\\.|Nov\\.|Dec\\.){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the forth date pattern: %s", line));
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
                    if(matcherCompany.find()){
                        report.setDate(matcherDate.group());
                        report.setCompanyName(matcherCompany.group(1));
                        report.setTicker(matcherCompany.group(2));
                    }
                    break;
                }
            }
        }

        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("[0-9]{1,2}\\s*/\\s*[0-9]{1,2}\\s*/\\s*[0-9]{1,4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the fifth date pattern: %s", line));
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
                    if(matcherCompany.find()){
                        report.setDate(matcherDate.group());
                        report.setCompanyName(matcherCompany.group(1));
                        report.setTicker(matcherCompany.group(2));
                    }
                    break;
                }
            }
        }

        if(report.getDate() == null || report.getDate().equals("")){
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate.matcher(line);
                if (matcherDate.find()) {
                    report.setDate(matcherDate.group());
                    break;
                }
            }
        }

        //Ticker
        if(report.getTicker() == null || report.getTicker().equals("")){
            Pattern patternTicker = Pattern.compile("Ticker Symbol\\s*([A-Z]+)\\s");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherTicker = patternTicker.matcher(line);
                if (matcherTicker.find()) {
                    report.setTicker(matcherTicker.group(1));
                    break;
                }
            }
        }

        Pattern patternThree = Pattern.compile("(.*)\\(([A-Z]+)\\s*\\-\\s*(\\$[0-9]+\\.[0-9]+)\\S\\s([A-Za-z]+)\\)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherThree = patternThree.matcher(line);
            if (matcherThree.find()) {
                if(report.getCompanyName() == null || report.getCompanyName().equals(""))
                    report.setCompanyName(matcherThree.group(1));
                report.setTicker(matcherThree.group(2));
                report.setRating(matcherThree.group(4));
                break;
            }
        }

        Pattern patternThree2 = Pattern.compile("(.*)\\(([A-Z]+)\\S*\\s*([A-Za-z]+).*(\\$[0-9]+\\.[0-9]+)\\)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherThree = patternThree2.matcher(line);
            if (matcherThree.find()) {
                if(report.getCompanyName() == null || report.getCompanyName().equals(""))
                    report.setCompanyName(matcherThree.group(1));
                report.setTicker(matcherThree.group(2));
                report.setRating(matcherThree.group(3));
                break;
            }
        }

        //Rating
        if(report.getRating() == null || report.getRating().equals("")){
            Pattern patternRating = Pattern.compile("Stock Rating\\s*([A-Za-z]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(1));
                    break;
                }
            }
        }
        if(report.getRating() == null || report.getRating().equals("")){
            Pattern patternRating = Pattern.compile("Rating\\s*([A-Za-z]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(1));
                    break;
                }
            }
        }
        if(report.getRating() == null || report.getRating().equals("")){
            Pattern patternRating = Pattern.compile("Rating:\\s*([A-Za-z]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(1));
                    break;
                }
            }
        }

        //Analyst name
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(),false);
        lines = content.split("\r\n");
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        Pattern patternName = Pattern.compile("(.*) \u0003 .* \u0003 .*@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if(StringUtil.isEmpty(line))
                continue;
            Matcher matcherEmail = patternEmail.matcher(line);
            Matcher matcherName = patternName.matcher(line);
            if (matcherEmail.find()) {
                if(matcherName.find()){
                    report.addAnalystName(matcherName.group(1));
                }
                else{
                    String nameLine = lines[i - 2];
                    report.addAnalystName(nameLine);
                }
            }
        }

        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
        lines = content.split("\r\n");
        List<String> disclosures = new ArrayList<String>();
        List<String> temp = new ArrayList<String>();
        String disclosure = "";
        Pattern patternDisclosure = Pattern.compile("hold(s)?.*position");
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            Matcher matcherDisclosure = patternDisclosure.matcher(line);
            if(matcherDisclosure.find()) {
                int j = i + 1, k = i - 1;
                //往上和往下找整句
                while(!lines[k].contains(".") && k >= 0){
                    temp.add(lines[k]);
                    k--;
                }
                if(k >= 0)
                    temp.add(lines[k]);
                for(int m = temp.size() - 1; m >= 0; m--){
                    disclosure += temp.get(m) + " ";
                }
                temp.clear();
                disclosure += line + " ";
                while(!lines[j].contains(".") && j < lines.length){
                    disclosure += lines[j] + " ";
                    j++;
                }
                if(j < lines.length)
                    disclosure += lines[j] + " ";
            }
        }
        Pattern patternSentence = Pattern.compile(".*([A-Z][a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+.*)");
        String[] values = disclosure.split("\\.");
        for(String value : values){
            System.out.println(value);
            Matcher matcherSentence = patternSentence.matcher(value);
            Matcher matcherDisclosure = patternDisclosure.matcher(value);
            if(matcherSentence.find() && matcherDisclosure.find()){
                disclosures.add(value);
                break;
            }
        }

        if(disclosures.size() == 0){
            for(String value : values){
                Matcher matcherDisclosure = patternDisclosure.matcher(value);
                if(matcherDisclosure.find()){
                    disclosures.add(value);
                    break;
                }
            }
        }

        if(disclosures.size() == 0){
            temp = new ArrayList<String>();
            disclosure = "";
            for(int i = 0; i < lines.length; i++){
                String line = lines[i].trim();
                if(line.contains("has a long position in")) {
                    int j = i + 1, k = i - 1;
                    //往上和往下找整句
                    while(!lines[k].contains(".") && k >= 0){
                        temp.add(lines[k]);
                        k--;
                    }
                    if(k >= 0)
                        temp.add(lines[k]);
                    for(int m = temp.size() - 1; m >= 0; m--){
                        disclosure += temp.get(m) + " ";
                    }
                    temp.clear();
                    disclosure += line + " ";
                    while(!lines[j].contains(".") && j < lines.length){
                        disclosure += lines[j] + " ";
                        j++;
                    }
                    if(j < lines.length)
                        disclosure += lines[j] + " ";
                }
            }
            patternSentence = Pattern.compile(".*([A-Z][a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+.*)");
            values = disclosure.split("\\.");
            for(String value : values){
                System.out.println(value);
                Matcher matcherSentence = patternSentence.matcher(value);
                if(matcherSentence.find() && value.contains("has a long position in")){
                    disclosures.add(value);
                    break;
                }
            }

            if(disclosures.size() == 0){
                for(String value : values){
                    if(value.contains("has a long position in")){
                        disclosures.add(value);
                        break;
                    }
                }
            }
        }

        report.setOwnershipDisclosures(disclosures);

        report.setPriorRecommendation("");
        report.setExchange("");
        report.setInitiatingCoverage("");
        System.out.println(JSON.toJSON(report));
    }

    public static void process(String rootDir, String resultPath) {
        List<AnalysisReport> reports = new ArrayList<>();
        File[] Files = new File(rootDir).listFiles();
        for (File file : Files) {
            if(file.getName().endsWith(".pdf"))
                reports.addAll(getAnalysisReportPageInfo(file.getPath()));
        }

        //以下代码为写入excel
        List<String> headers = new ArrayList<>();
        headers.add("Date");
        headers.add("Broker name");
        headers.add("Analyst name");
        headers.add("Company name");
        headers.add("Ticker");
        headers.add("Exchange");
        headers.add("Rating");
        headers.add("Initiating Coverage");
        headers.add("Prior recommendation");
        headers.add("Ownership disclosure");
        headers.add("Company Mentioned");
        headers.add("PDF file name");
        headers.add("StartPage");
        headers.add("EndPage");
        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);

        List<String[]> data = new ArrayList<>();
        for (AnalysisReport report : reports) {
            try {
                getAnalysisReportDetail(report);
            } catch (Exception e) {
                System.out.println(JSON.toJSON(report));
                e.printStackTrace();
                continue;
            }

            String[] line = new String[]{report.getDate(),
                    report.getBrokerName(),
                    JSON.toJSONString(report.getAnalystNames()),
                    report.getCompanyName(),
                    report.getTicker(),
                    report.getExchange(),
                    report.getRating(),
                    report.getInitiatingCoverage(),
                    report.getPriorRecommendation(),
                    JSON.toJSONString(report.getOwnershipDisclosures()),
                    JSON.toJSONString(report.getCompaniesMentioned()),
                    report.getFileName(),
                    report.getStartPage() + "",
                    report.getEndPage() + ""};
            data.add(line);
        }

        FileUtil.exportSheet(sheetModel.getSheet(), data);
        FileUtil.closeSheet(sheetModel);
        System.out.println("Success!");
    }
}
